package com.example.cdpbookstore.dao.impl;

import com.example.cdpbookstore.dao.BookDao;
import com.example.cdpbookstore.pojo.entity.Book;
import com.example.cdpbookstore.tool.C3P0Tool;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class BookDaoImpl implements BookDao {
    @Override
    public List<Book> list() {
        String sql = "select * from tb_book";
        try {
            return C3P0Tool.queryRunner.query(sql, new BeanListHandler<>(Book.class));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Book> list(Long id) {
        String sql = "select b.* from tb_book b left join tb_category c on b.categoryId=c.id where c.id=?";
        try {
            return C3P0Tool.queryRunner.query(sql, new BeanListHandler<>(Book.class), id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Book getById(Long id) {
        String sql = "select * from tb_book where id=?";
        try {
            return C3P0Tool.queryRunner.query(sql, new BeanHandler<>(Book.class), id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean update(Book book) {
        String sql = "update tb_book set categoryId=?, bookName=?, author=?, press=?, currentPrice=?, originalPrice=?, " +
                "isbn=?, images=?, stock=?, state=?, creationTime=?, updateTime=? where id=?";
        try {
            int update = C3P0Tool.queryRunner.update(sql,
                    book.getCategoryId(),
                    book.getBookName(),
                    book.getAuthor(),
                    book.getPress(),
                    book.getCurrentPrice(),
                    book.getOriginalPrice(),
                    book.getIsbn(),
                    book.getImages(),
                    book.getStock(),
                    book.getState(),
                    book.getCreationTime(),
                    book.getUpdateTime(),
                    book.getId());
            return update > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int getSalesVolume(Long id) {
        String sql = "select sum(num) `salesVolume` from tb_order_detail where bookId=?";

        try {
            Map<String, Object> query = C3P0Tool.queryRunner.query(sql, new MapHandler(), id);
            Object salesVolume = query.get("salesVolume");
            if (salesVolume == null) {
                return 0;
            } else {
                return ((BigDecimal) salesVolume).intValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<Book> search(String search) {
        String sql = "select * from tb_book where bookName like ? or author like ? or press like ?";
        String searchText = "%" + search + "%";
        try {
            return C3P0Tool.queryRunner.query(sql, new BeanListHandler<>(Book.class),
                    searchText,
                    searchText,
                    searchText);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
